#coding=utf-8
import re
import os

# python默认读取文本文件
# 如在windows上，当读取时自动将\r\n换成\n;写入时自动将\n换成\r\n
# 在mac上也是类似。但在linux上这不是问题

def test2():
	f = open('tmp.txt','w')
	f.close()
	f = open('tmp.txt','r+')
	f.write('hello,')
	#f.seek(5)
	f.write('\nworld')
	f.close()

	print '--- after write:'
	f = open('tmp.txt','rb')
	print f.readline()

	print '--- calling f.read()'
	print f.read()
	f.close()

def rename(path):
	files = os.listdir(path)
	index = 1
	for file in files:
		filename = os.path.splitext(file)[0][:]
		print filename
		# suffix = os.path.splitext(file)[1]

        # srcpath = os.path.join(path,file)d
        #print re.split(r'\d+',filename)
		index += 1

def renameFiles(path,prefix):
    files = os.listdir(path)
    index = 1
    for file in files:
        filename = os.path.splitext(file)[0][:]
        suffix = os.path.splitext(file)[1]

        srcpath = os.path.join(path,file)
        destpath = path + '\\' + prefix + '_%04d'%(index) + suffix
        os.rename(srcpath,destpath)

        index += 1

def test1():
	path = u'E:\\BaiduYunDownload\\violin\\Suzuki-violin'
	path1 = u'E:\\聆听音乐'
	path2 = 'D:\\violin'
	rename(path2)

test2()
